package com.imooc.utils.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import lombok.extern.slf4j.Slf4j;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;
@Slf4j
public class Date2LongSerializer extends JsonSerializer {

    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        try{
            Date date = (Date) o;
            jsonGenerator.writeNumber(date.getTime() / 1000);
        }catch (Exception e){
            log.error("【时间转换失败】不是Date类型，o ：{}",o);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

    }
}
